package Android.Billarm;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout;
import java.text.SimpleDateFormat;

import java.util.HashMap;
import java.util.ArrayList;

public class CalendarScreen extends Activity
{
    public static final int SHOW_ALL_BILLS = 1;
    public static final int SHOW_ACTIVE_BILLS = 2;
    public static final int SHOW_INACTIVE_BILLS = 3;

    private BillAdapter adapter;  
    private DB_SQLite db;
    private int mode;
    private boolean paused = false;

    /** Called when the activity is first created. */
    protected void onCreate(Bundle state)
    {
        super.onCreate(state);
        setContentView(R.layout.calendar_list);

        mode = getIntent().getIntExtra ("show", SHOW_ALL_BILLS);

        db = new DB_SQLite (this);
        db.open ();
        Cursor cursor;
        switch (mode)
        {
            case SHOW_ACTIVE_BILLS:
                cursor = db.fetchActiveBillarms ();
                break;
            case SHOW_INACTIVE_BILLS:
                cursor = db.fetchInactiveBillarms ();
                break;
            case SHOW_ALL_BILLS:
            default:
                cursor = db.fetchAllBillarms ();
        };
        adapter = new BillAdapter (this, cursor);

        ListView list = (ListView) findViewById (R.id.calendar_list);
        list.setOnItemClickListener (listItemClickListener);
        list.setAdapter (adapter);
    }  

    protected void onPause ()
    {
        paused = true;
        super.onPause ();
    }

    protected void onResume ()
    {
        super.onResume ();
        if (paused)
            /* The database may have updated */
        {
            adapter.getCursor() .requery();
            adapter.notifyDataSetChanged ();
        }
        paused = false;
    }

    public void doneClicked (View v) { finish (); }

    private AdapterView.OnItemClickListener listItemClickListener = new AdapterView.OnItemClickListener()
    {
        public void onItemClick (AdapterView<?> l, View v, int pos, long id)
        {
            Intent seebill = new Intent (l.getContext(), BillScreen.class);
            /* Send the Bill to BillScreen */
            seebill.putExtra ("bill", adapter.getBill (pos));
            startActivity (seebill);
        }
    };

    protected void onDestroy ()
    {
        Log.d ("CalendarScreen", "destroy!");
        db.close ();
        adapter.close ();
        super.onDestroy ();
    }

    /* This cycles thru the bills for the list */
    private class BillAdapter extends android.widget.CursorAdapter
    {
        public BillAdapter (Context context, Cursor cursor)
        { super (context, cursor, true); }

        public Bill getBill (Cursor cursor)
        { return DB_SQLite.cursorToBill (cursor); }

        public Bill getBill (int i)
        {
            Cursor c = getCursor ();
            c.moveToPosition (i);
            return getBill (c);
        }

        public void close ()
        { getCursor() .close(); }


        private void setBillView (Cursor cursor, ViewGroup group, Context context)
        {
            Bill bill = getBill (cursor);
            TextView text = (TextView) group.findViewById (R.id.calItemDate);
            SimpleDateFormat d = new SimpleDateFormat ("EEE, MMM d, yyyy,  h:mm a");
            text.setText (d.format (bill.getBillarmDate() .getTime()));

            {
                DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);

                text.setBackgroundDrawable
                    (BillIcon.catIcons
                     (bill.icons, dm.widthPixels, getResources ()));
            }

            /*
            for (int icon : bill.icons)
            {
                ImageView im = new ImageView (context);
                BillIcon.setImageView (icon, im);
                group.setBackckground
                group.addView (im);
            }
            */
        }

        public void bindView (View view, Context context, Cursor cursor)
        {
            ViewGroup group = (ViewGroup) view;
            setBillView (cursor, group, context);
        }

        public View newView (Context context, Cursor cursor, ViewGroup parent)
        {
            ViewGroup group = (ViewGroup) CalendarScreen.this.getLayoutInflater ()
                .inflate (R.layout.calendar_list_item, null);

            setBillView (cursor, group, context);
            return group;
        }
    }
}


